import axios from "axios";
import {admin, crowd, publisher} from "@/store/pojo/Position";
import {IUser} from "@/store/pojo/IUser";
import router from "@/router";
import {UserStoreModule} from "@/store/modules/User";

const update = async (user: IUser) => {
    const position = user.position
    let url = "/api/";
    if (position == crowd) {
        url += crowd.toString();
    } else if (position == admin) {
        url += admin.toString();
    } else if (position == publisher) {
        url += publisher.toString();
    }
    url += "/update";

    const id = user.id;
    const username = user.username;
    const password = user.password;
    const phone = user.phone;
    const email = user.email;



    return axios.post(url, {
        id,
        username,
        password,
        phone,
        email
    }).then(function (response) {
        //导航至登陆页面
        if (response.data == true){
            alert("更新成功,即将跳转到个人主页");
            UserStoreModule.setUserInfoToSessionStorage(user);
        }else {
            alert("更新失败，回到个人主页");
        }
        router.push({
            path: `/user/${position}/workspace`,
            query: {
                userId: id
            }
        });

    })
}

export default update;